import 'package:flutter/material.dart';
import 'package:quick_dev_plugin/utils/toast_utils.dart';

///author: create by SunJH
///时间: 2022/1/17 0017
///邮箱: 965989158@qq.com
///描述:

class AppFloatBox extends StatefulWidget {
  final Container child;
  final int width;
  final int height;
  AppFloatBox(this.width,this.height,{Key? key,required this.child}):super(key:key);

  final _AppFloatBoxState _appFloatBoxState = _AppFloatBoxState();

  @override
  _AppFloatBoxState createState() => _appFloatBoxState;

  refShowOrGoneFloatBox(bool isShowFloatBox){
    _appFloatBoxState.refShowOrGoneFloatBox(isShowFloatBox);
  }
}

class _AppFloatBoxState extends State<AppFloatBox> {
  bool showFloatBox = true;
  Offset offset = const Offset(10, kToolbarHeight + 10);
  Offset _calOffset(Size size, Offset offset, Offset nextOffset) {
    double dx = 0;
    //水平方向偏移量不能小于0不能大于屏幕最大宽度
    if (offset.dx + nextOffset.dx <= 0) {
      dx = 0;
    } else if (offset.dx + nextOffset.dx >= (size.width - widget.width)) {
      dx = size.width - widget.width;
    } else {
      dx = offset.dx + nextOffset.dx;
    }
    double dy = 0;
    //垂直方向偏移量不能小于0不能大于屏幕最大高度
    if (offset.dy + nextOffset.dy >= (size.height - widget.height)) {
      dy = size.height - widget.height;
    } else if (offset.dy + nextOffset.dy <= kToolbarHeight) {
      dy = kToolbarHeight;
    } else {
      dy = offset.dy + nextOffset.dy;
    }
    return Offset(
      dx,
      dy,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
        left: offset.dx,
        top: offset.dy,
        child:showFloatBox? GestureDetector(
            onPanUpdate: (detail) {
              setState(() {
                offset =
                    _calOffset(MediaQuery.of(context).size, offset, detail.delta);
              });
            },
            child: widget.child,
        ):const SizedBox()
    );
  }

  refShowOrGoneFloatBox(bool isShowFloatBox){
    setState(() {
      showFloatBox = isShowFloatBox;
    });
  }
}

